package com.android.job.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.fusion.FusionField;
import com.android.job.logic.RegisterLogic;
import com.android.job.model.LoginInfo;
import com.android.job.util.json.JSONObject;

public class RegisterActivity extends BaseActivity implements OnClickListener
{

	private EditText identityET;
	private EditText userPassET;
	private EditText userNameET;
	private TextView sexTV;
	private EditText phoneET;
	private TextView regionTV;
	private TextView typeTV;
	private TextView natureTV;
	private TextView degreeTV;
	private TextView categorieTV;
	private TextView politicalTV;
	private TextView salaryTV;

	private String identity="";
	private String password="";
	private String name="";
	private String sex="";
	private String phone="";
	private String region="";
	private String type="";
	private String nature="";
	private String degree="";
	private String categorie="";
	private String political="";
	private String salary="";

	private Button subBtn;
	private Button backBtn;
//	private CheckBox saveCB;

	private RelativeLayout sex_layout;
	private RelativeLayout region_layout;
	private RelativeLayout type_layout;
	private RelativeLayout nature_layout;
	private RelativeLayout degree_layout;
	private RelativeLayout categorie_layout;
	private RelativeLayout salary_layout;
	private RelativeLayout political_layout;
	private ProgressDialog pd;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initLayout();
	}

	private void initLayout()
	{
		identityET = (EditText) findViewById(R.id.register_identity_tv_et);
		userNameET = (EditText) findViewById(R.id.register_user_name_et);
		userPassET = (EditText) findViewById(R.id.register_user_password_et);
		sexTV = (TextView) findViewById(R.id.register_user_sex_et);
		phoneET = (EditText) findViewById(R.id.register_user_phone_et);
		regionTV = (TextView) findViewById(R.id.register_region_et);
		typeTV = (TextView) findViewById(R.id.register_type_et);
		natureTV = (TextView) findViewById(R.id.register_nature_et);
		degreeTV = (TextView) findViewById(R.id.register_degree_et);
		categorieTV = (TextView) findViewById(R.id.register_categories_et);
		politicalTV = (TextView) findViewById(R.id.register_political_et);
		salaryTV = (TextView) findViewById(R.id.register_salary_et);
		subBtn = (Button) findViewById(R.id.register_btn);
//		saveCB = (CheckBox) findViewById(R.id.save_checkbox);
		subBtn.setOnClickListener(this);

		backBtn = (Button) findViewById(R.id.register_back);
		backBtn.setOnClickListener(this);
		sex_layout = (RelativeLayout) findViewById(R.id.region_layout);
		sex_layout.setOnClickListener(this);

		region_layout = (RelativeLayout) findViewById(R.id.register_region_layout);
		region_layout.setOnClickListener(this);

		type_layout = (RelativeLayout) findViewById(R.id.register_type_layout);
		type_layout.setOnClickListener(this);

		nature_layout = (RelativeLayout) findViewById(R.id.register_nature_layout);
		nature_layout.setOnClickListener(this);

		degree_layout = (RelativeLayout) findViewById(R.id.register_degree_layout);
		degree_layout.setOnClickListener(this);

		categorie_layout = (RelativeLayout) findViewById(R.id.register_categories_layout);
		categorie_layout.setOnClickListener(this);

		salary_layout = (RelativeLayout) findViewById(R.id.register_salary_layout);
		salary_layout.setOnClickListener(this);
		
		political_layout = (RelativeLayout) findViewById(R.id.register_political_layout);
		political_layout.setOnClickListener(this);
		sharedPreferences = getSharedPreferences(FusionField.POSTARR,
				Activity.MODE_PRIVATE);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.register_btn:
			register();
			break;
		case R.id.region_layout:
			jumpToSelect(FusionCode.GENDER_ITEM, R.id.register_user_sex_et);
			break;
		case R.id.register_region_layout:
			jumpToSelect(FusionCode.CITY_ITEM_ONE, R.id.register_region_et);
			break;
		case R.id.register_type_layout:
			jumpToSelect(FusionCode.JOB_TYPE_ITEM, R.id.register_type_et);
			break;
		case R.id.register_nature_layout:
			jumpToSelect(FusionCode.NATURE_ITEM, R.id.register_nature_et);
			break;
		case R.id.register_degree_layout:
			jumpToSelect(FusionCode.DEGREE_ITEM, R.id.register_degree_et);
			break;
		case R.id.register_categories_layout:
			jumpToSelect(FusionCode.CATEGORIES_ITEM, R.id.register_categories_et);
			break;
		case R.id.register_salary_layout:
			jumpToSelect(FusionCode.SALARY_ITEM, R.id.register_salary_et);
			break;
		case R.id.register_political_layout:
			jumpToSelect(FusionCode.POLITICS_ITEM, R.id.register_political_et);
			break;
		case R.id.register_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == 0 && resultCode == 0)
		{
			if (data != null)
			{
				int id = data.getIntExtra("view_id", 0);
				String choose = data.getStringExtra("chose");
				String text = data.getStringExtra("text");
				switch (id)
				{
				case R.id.register_user_sex_et:
					sex = choose;
					sexTV.setText(text);
					break;
				case R.id.register_region_et:
					region = choose;
					regionTV.setText(text);
					break;
				case  R.id.register_type_et:
					type = choose;
					typeTV.setText(text);
					break;
				case R.id.register_nature_et:
                     nature = choose;
                     natureTV.setText(text);
					break;
				case R.id.register_degree_et:
					degree = choose;
					degreeTV.setText(text);
					break;
				case R.id.register_salary_et:
                    salary = choose;
                    salaryTV.setText(text);
					break;
				case R.id.register_categories_et:
					categorie = choose;
					categorieTV.setText(text);
					break;
				case R.id.register_political_et:
					political = choose;
					politicalTV.setText(text);
					break;
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);

	}
	
	
	private void register()
	{
		identity = identityET.getText().toString().trim();
		password = userPassET.getText().toString().trim();
		name = userNameET.getText().toString().trim();
//		sex = sexTV.getText().toString().trim();
		phone = phoneET.getText().toString().trim();
//		region = regionTV.getText().toString().trim();
//		type = typeTV.getText().toString().trim();
//		nature = natureTV.getText().toString().trim();
//		degree = degreeTV.getText().toString().trim();
//		categorie = categorieTV.getText().toString().trim();
//		political = politicalET.getText().toString().trim();
//		salary = salaryTV.getText().toString().trim();

		if (identity == null || "".equals(identity))
		{
			Toast.makeText(this, R.string.check_identity, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (password == null || "".equals(password))
		{
			Toast.makeText(this, R.string.check_password, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (name == null || "".equals(name))
		{
			Toast.makeText(this, R.string.check_name, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (sex == null || "".equals(sex))
		{
			Toast.makeText(this, R.string.check_sex, Toast.LENGTH_SHORT).show();
			return;
		}
		if (phone == null || "".equals(phone))
		{
			Toast.makeText(this, R.string.check_phone, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (region == null || "".equals(region))
		{
			Toast.makeText(this, R.string.check_region, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (type == null || "".equals(type))
		{
			Toast.makeText(this, R.string.check_type, Toast.LENGTH_SHORT)
					.show();
			return;
		}
//		if (nature == null || "".equals(nature))
//		{
//			Toast.makeText(this, R.string.check_nature, Toast.LENGTH_SHORT)
//					.show();
//			return;
//		}
//		if (degree == null || "".equals(degree))
//		{
//			Toast.makeText(this, R.string.check_nature, Toast.LENGTH_SHORT)
//					.show();
//			return;
//		}
//		if (categorie == null || "".equals(categorie))
//		{
//			Toast.makeText(this, R.string.check_categories, Toast.LENGTH_SHORT)
//					.show();
//			return;
//		}
//		if (political == null || "".equals(political))
//		{
//			Toast.makeText(this, R.string.check_political, Toast.LENGTH_SHORT)
//					.show();
//			return;
//		}
		if (salary == null || "".equals(salary))
		{
			Toast.makeText(this, R.string.check_salary, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		pd = new ProgressDialog(RegisterActivity.this);
		pd.setCancelable(true);
		pd.setMessage("注册中...");
		pd.show();
		RegisterLogic.getInstance().register(handler, password, name, sex,
				identity, phone, degree, categorie, political, salary, region,
				type, nature, "", "", "", "", "", "", "", "", "");
	}

	private Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case FusionCode.NETWORK_ERROR:
				pd.dismiss();
				Toast.makeText(RegisterActivity.this, R.string.network_error,
						Toast.LENGTH_SHORT).show();
				break;
			case FusionCode.NETWORK_TIMEOUT_ERROR:
				pd.dismiss();
				Toast.makeText(RegisterActivity.this, R.string.network_timeout,
						Toast.LENGTH_SHORT).show();
				break;
			case FusionCode.RETURN_JSONOBJECT:
				pd.dismiss();
////				JSONObject jsonObject = (JSONObject) msg.obj;
////				Log.e("free.lang", "=========" + jsonObject.toString());
//				FusionField.islogin = true;
				
				LoginInfo info = (LoginInfo) msg.obj;
				saveData(info);
				Toast.makeText(RegisterActivity.this,"注册成功！",
						Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(RegisterActivity.this, HomeActivity.class);
				finish();
				RegisterActivity.this.startActivity(intent);
				break;
			case FusionCode.SERVER_ERROR:
				pd.dismiss();
				String errorInif = (String) msg.obj;
				
				Toast.makeText(RegisterActivity.this,
						errorInif, Toast.LENGTH_SHORT)
						.show();
				break;
			}
		};
	};

	private void saveData(LoginInfo data)
	{
		if(sharedPreferences!=null)
		{
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putString("type",data.userType);
			editor.putString("id",data.userId);
			editor.commit();
		}
		
	}
}
